package com.zczc.web.controller;

import com.github.pagehelper.PageInfo;
import com.yishuifengxiao.common.tool.response.Response;
import com.yishuifengxiao.common.tool.response.Response.Const;
import com.zczc.entity.MessageGetter;
import com.zczc.service.MessageGetterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 15:51 2018-12-13
 * @Modify by:
 */
@RestController
@Api(tags = "用户获取消息接口")
@RequestMapping("/zczc/messageGetter")
@Validated
public class MessageGetterController extends BaseController {

  private final static Logger log= LoggerFactory.getLogger(MessageGetterController.class);



  @ApiOperation(value = "根据用户id,向用户获取表中添加消息,当用户登陆时调用此接口")
  @PostMapping("/addMessageByUser")
  public Response addMessageByUser(
      @ApiParam(required = true,name = "userId",value = "用户id") @RequestParam(required = true,value = "userId") @NotEmpty(message = "userId不能为空") @NotNull(message = "userId不能为null") String userId){
    MessageGetter messageGetter=new MessageGetter();
    messageGetter.setUserId(convert(userId));
    try {
      messageGetterService.addMessageGetterSelective(messageGetter);
      return Response.suc();
    } catch (Exception e) {
      log.info("向用户获取表中添加消息报错==================》"+e.getMessage());
      return Response.error(e.getMessage());
    }
  }

  @ApiOperation(value = "根据用户id值分页显示所有的消息")
  @GetMapping("getMessageByUserPage")
  public Response<PageInfo<MessageGetter>> getMessageByUserPage(
      @ApiParam(required = true,name = "userId",value = "用户id") @RequestParam(required = true,value = "userId") @NotEmpty(message = "userId不能为空") @NotNull(message = "userId不能为null")String userId,
      @ApiParam(required = false,name = "messageTitle",value = "根据消息标题进行查询") @RequestParam(required = false,value = "messageTitle") String messageTitle,
      @ApiParam(required = false,name = "messageContent",value = "根据消息内容进行查询") @RequestParam(required = false,value = "messageContent") String messageContent,
      @ApiParam(required = false,name = "pageNum",value = "页码，默认为1") @RequestParam(required = false,value = "pageNum") @Min(value = 1,message = "pageNum最小为1")Integer pageNum,
      @ApiParam(required = false,name = "pageSize",value = "页大小，默认为20") @RequestParam(required = false,value = "pageSize") @Min(value = 1,message = "页大小最小为1")Integer pageSize){
    MessageGetter messageGetter=new MessageGetter();
    messageGetter.setUserId(userId);
    messageGetter.setMessageTitle(convert(messageTitle));
    messageGetter.setMessageContent(convert(messageContent));
    try {
      PageInfo<MessageGetter> data = messageGetterService
          .selectMessageByUserId(messageGetter, pageNum, pageSize);
      return new Response< PageInfo<MessageGetter>>(Const.CODE_OK,Const.MSG_OK,data);
    } catch (Exception e) {
     log.info("向用户获取表中添加消息报错==================》"+e.getMessage());
      return new Response<>(Const.CODE_INTERNAL_SERVER_ERROR,e.getMessage(),null);
    }
  }

  @ApiOperation(value = "根据用户id值批量删除消息")
  @DeleteMapping("deleteMessageByUser")
  public Response deleteMessageByUser(@ApiParam(required = true,name = "ids",value = "要删除的id值")
  @RequestBody(required = true) @NotNull(message = "ids不能为null") @NotEmpty(message = "ids不能为空") List<String> ids,
  @ApiParam(required = true,name = "userId",value = "用户id") @RequestParam(required = true,value = "userId") @NotEmpty(message = "userId不能为空") @NotNull(message = "userId不能为null")String userId){
    messageGetterService.deleteMessageByIdLogic(ids,userId);
    return Response.suc();
  }


  @ApiOperation(value = "根据用户id值批量阅读消息")
  @PutMapping("readMessageByUser")
  public Response readMessageByUser(@ApiParam(required = true,name = "ids",value = "已阅读的id值")
  @RequestBody(required = true) @NotNull(message = "ids不能为null") @NotEmpty(message = "ids不能为空") List<String> ids,
      @ApiParam(required = true,name = "userId",value = "用户id") @RequestParam(required = true,value = "userId") @NotEmpty(message = "userId不能为空") @NotNull(message = "userId不能为null")String userId){
    messageGetterService.readnMessageById(ids,userId);
    return Response.suc();
  }




}
